/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2011-2020 Rock Mao
* creator : Rock Mao
* created : 2011-10-25   22:29
* filename: LaborD3D11ConstantBuffer.cpp
-----------------------------------------------------------------
*/
#include "stdafx.h"
#include "LaborD3D11ConstantBuffer.h"
#include "LaborD3D11Shader.h"
NAMESPACE_LABOR3D_BEGIN
//---------------------------------------------------------------------
CLD3D11ConstantBuffer::CLD3D11ConstantBuffer(size_t buffer_size, CLD3D11Device & device) : CLConstantBuffer(buffer_size)
{
	m_pReflection = NULL;
	mSizeInBytes = buffer_size;
	// everything is done via internal generalisation
	mBufferImpl = new CLD3D11HardwareBuffer(eL_CONSTANT_BUFFER, mSizeInBytes, mUsage, device, false, false);

}

//---------------------------------------------------------------------
CLD3D11ConstantBuffer::~CLD3D11ConstantBuffer()
{
	delete mBufferImpl;
}

//---------------------------------------------------------------------
void* CLD3D11ConstantBuffer::lock(size_t offset, size_t length, EHB_LockOptions options)
{
	return mBufferImpl->lock(offset, length, options);
}
//---------------------------------------------------------------------
void CLD3D11ConstantBuffer::unlock(void)
{
	mBufferImpl->unlock();
}
//---------------------------------------------------------------------
void CLD3D11ConstantBuffer::readData(size_t offset, size_t length, void* pDest)
{
	mBufferImpl->readData(offset, length, pDest);
}
//---------------------------------------------------------------------
void CLD3D11ConstantBuffer::writeData(size_t offset, size_t length, const void* pSource,
								   bool discardWholeBuffer)
{
	mBufferImpl->writeData(offset, length, pSource, true);
}
//---------------------------------------------------------------------
void CLD3D11ConstantBuffer::copyData(CLHardwareBuffer& srcBuffer, size_t srcOffset, 
								  size_t dstOffset, size_t length, bool discardWholeBuffer)
{
	CLD3D11ConstantBuffer& d3dBuf = static_cast<CLD3D11ConstantBuffer&>(srcBuffer);

	mBufferImpl->copyData(*(d3dBuf.mBufferImpl), srcOffset, dstOffset, length, discardWholeBuffer);
}
//---------------------------------------------------------------------
bool CLD3D11ConstantBuffer::isLocked(void) const
{
	return mBufferImpl->isLocked();
}
//---------------------------------------------------------------------
bool CLD3D11ConstantBuffer::releaseIfDefaultPool(void)
{
	/*		if (mD3DPool == D3DPOOL_DEFAULT)
	{
	SAFE_RELEASE(mlpD3DBuffer);
	return true;
	}
	return false;
	*/
	return true;
}
//---------------------------------------------------------------------
bool CLD3D11ConstantBuffer::recreateIfDefaultPool(CLD3D11Device & device)
{

	return true;
}
//---------------------------------------------------------------------
ID3D11Buffer * CLD3D11ConstantBuffer::getD3DConstantBuffer( void ) const
{
	return mBufferImpl->getD3DBuffer();
}

//---------------------------------------------------------------------
CLD3D11HDBufReflection* CLD3D11ConstantBuffer::reflection()
{
    return m_pReflection;
}


CLD3D11HDBufReflection* CLD3D11ConstantBuffer::createReflection(bool bSysBuffer)
{
	if(m_pReflection == NULL)
	{
		m_pReflection = new CLD3D11HDBufReflection(this , bSysBuffer , (int)m_nBufferSize);
	}
	return m_pReflection;
}

NAMESPACE_LABOR3D_END